var itemcat = new Object();

itemcat.isAdmin = false;

itemcat.loadRegions = function(parent, type, target){   
    Ajax.call(itemcat.getFileName(), 'type=' + type + '&target=' + target + "&parent=" + parent , itemcat.response, "GET", "JSON");
}

/* *
 * 载入指定的国家下所有的省份
 *
 * @country integer     国家的编号
 * @selName string      列表框的名称
 */
itemcat.loadProvinces = function(country, selName)
{
  var objName = (typeof selName == "undefined") ? "selProvinces" : selName;

  itemcat.loadRegions(country, 1, objName);
}

/* *
 * 载入指定的省份下所有的城市
 *
 * @province    integer 省份的编号
 * @selName     string  列表框的名称
 */
itemcat.loadCities = function(province, selName)
{
  var objName = (typeof selName == "undefined") ? "selCities" : selName;

  itemcat.loadRegions(province, 2, objName);
}

/* *
 * 载入指定的城市下的区 / 县
 *
 * @city    integer     城市的编号
 * @selName string      列表框的名称
 */
itemcat.loadDistricts = function(city, selName)
{
  var objName = (typeof selName == "undefined") ? "selDistricts" : selName;

  itemcat.loadRegions(city, 3, objName);
}

/* *
 * 处理下拉列表改变的函数
 *
 * @obj     object  下拉列表
 * @type    integer 类型
 * @selName string  目标列表框的名称
 */
itemcat.changed = function(obj, type, selName)
{
 // alert(obj.options[obj.selectedIndex].value  );
  var parent = obj.options[obj.selectedIndex].value; 
  itemcat.loadRegions(parent, type, selName);
}

itemcat.response = function(result, text_result){   
  var sel = document.getElementById(result.target); 
  sel.length = 1;
  sel.selectedIndex = 0; 
  //sel.style.display = (result.regions.length == 0 && ! region.isAdmin && result.type + 0 == 3) ? "none" : '';
  sel.style.display = (result.regions.length == 0  && result.type + 0 == 3) ? "none" : '';  
   if (result.regions)
  { 
    for (i = 0; i < result.regions.length; i ++ )
    { 
      var opt = document.createElement("OPTION");
      opt.value = result.regions[i].cid;
      opt.text  = result.regions[i].name; 
      sel.options.add(opt);
    }
    if( result.regions.length ==0 ){ 
    	 	if(result.target  == 'itemcat2'){ 
			  document.getElementById('goods_class').value = document.getElementById('itemcat1').value   ;   //二 无子类
			}else if(  result.target  == 'itemcat3' ){  //3 无子类
				document.getElementById('goods_class').value = document.getElementById('itemcat2').value   ;   //三 无子类
			}else if(  result.target  == 'itemcat4' ){  //4 无子类
				document.getElementById('goods_class').value = document.getElementById('itemcat3').value   ;   //四 无子类 
			}else{
			  document.getElementById('goods_class').value=0
			} 
    }else{
    	   if (document.all) { 
		     sel.fireEvent("onchange");
		  } else{  
		    var evt = document.createEvent("HTMLEvents");
		    evt.initEvent('change', true, true);
		    sel.dispatchEvent(evt);
		  } 
    }
  } 
  
}

itemcat.getFileName = function()
{
  if (itemcat.isAdmin)
  {
    return "itemcat.php";
  }
  else
  {
    return "itemcat.php";
  }
}
